<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2019/6/11
 * Time: 10:47
 */

namespace App\Http\Controllers\Admin\Api;


use App\Http\Controllers\Controller;
use App\Http\Repositories\Services\IType;
use App\Http\Requests\Admin\TypeInputDTO;
use App\Util\LayUiVo;
use App\Util\ResultsVo;
use Illuminate\Support\Facades\Lang;

class TypeController extends Controller
{
    public $typeService;

    public function __construct(IType $typeService)
    {
        $this->typeService = $typeService;
    }

    public function page() {
        $dao = $this->typeService->findTree();
        $data = [];
        if ($dao) {
            foreach ($dao as $key => $val){
                $val['name'] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', intval($val['lv'])).$val['name'];
                $data['s_'.$key] = $val;
            }
        }
        return response()->json(new LayUiVo(['data'=>$data,'message'=>Lang::get('response.success')]));
    }

    public function store(TypeInputDTO $createDTO){
        $this->typeService->store($createDTO);
        return response()->json(new ResultsVo(['data'=>true,'message'=>Lang::get('response.success'),'jump'=>'/admin/view/types']));
    }

    public function update(TypeInputDTO $updateDTO,$id){
        $this->typeService->update($updateDTO,$id);
        return response()->json(new ResultsVo(['data'=>true,'message'=>Lang::get('response.success'),'jump'=>'/admin/view/types']));
    }
}